//
//  ShoppingViewController.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/2.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit
import WebKit


class ShoppingViewController: UIViewController,WKUIDelegate,WKScriptMessageHandler,WKNavigationDelegate {
    
    var webView: WKWebView!
    
    //全局链接变量
    var url : String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //背景色--白色
        view.backgroundColor = UIColor.white
        
        //取出本地token，进行拼接
        if (localSave.object(forKey: userToken) != nil) {
            //加载购物车链接
            self.navigationItem.title = "购物车"
            
            //配置webview
            let configuration = WKWebViewConfiguration()
            
            
            // 设置偏好设置
            let preferences = WKPreferences()
            // 在iOS上默认为NO，表示不能自动通过窗口打开
            preferences.javaScriptCanOpenWindowsAutomatically = true
            preferences.minimumFontSize = 10.0
            configuration.preferences = preferences
            
            configuration.processPool = WKProcessPool()
            webView = WKWebView.init(frame: view.bounds, configuration: configuration)
            
            webView.uiDelegate = self
            webView.navigationDelegate = self
            
            
            if shoppingCarArray.count > 1 {
                let urlStr = shoppingCarArray.lastObject
                
                webView.load(URLRequest.init(url: URL.init(string: urlStr as! String)!))
            } else {
                DispatchQueue.main.async {
                    let url = URL.init(string: shooppingCarURL)
                    
                    self.webView.load(URLRequest.init(url: url!))
                }
            }
            
            view.addSubview(webView)
            
            
        } else {
            let alertView = UIAlertController.init(title: "提示", message: "请登录", preferredStyle: .alert)
            alertView.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil))
            
            alertView.addAction(UIAlertAction.init(title: "确定", style: .default, handler: { (nil) in
                
                self.navigationController?.pushViewController(LoginView(), animated: true)
            }))
            
            self.navigationController?.present(alertView, animated: true, completion: nil)
        }
    }
    
    /**
     *  JS 调用 swift 时 webview 会调用此方法
     *
     *  @param userContentController  webview中配置的userContentController 信息
     *  @param message                JS执行传递的消息
     */
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        print(url)
        
        print(shoppingCarArray.count)
        
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            
            
            //如果为本身或者本身携带#的url进行自身刷新。
            if url == shooppingCarURL {
                
                self.webView.load(URLRequest.init(url: URL.init(string: url)!))
                
                shoppingCarArray.removeAllObjects()
                
            } else {
                print(shoppingCarArray)
                shoppingCarArray.add(url)
                aaa(str: shoppingCarArray.lastObject as! String)
            }
            
            decisionHandler(.cancel)

        } else {
            decisionHandler(.allow)
        }
    }
    
    //url---
    func aaa(str : String) -> Void {
        
        let vvv = ShoppingReplaceView()
        vvv.url = str
        self.navigationController?.pushViewController(vvv, animated: true)
        
    }
    
    
    //获取标题
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.navigationItem.title = self.webView.title
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(.allow)
    }

}

